package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class OrderStatisticsTask {
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;

    @Scheduled(cron="00 00 02 * * ?")
    public void savaOrederStatistics(){
        //获取昨天的日期,并将它转换为一个字符串
        String yesterday = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");
        //调用OderInfoMapper中的订单方法
        OrderStatistics orderStatistics=orderInfoMapper.getYseterdayOderInfoDate(yesterday);
        //将统计数据保存到数据库中
        if(orderStatistics!=null){
            //调用OrederStatisticsMapper中保存的方法
            orderStatisticsMapper.save(orderStatistics);
        }
    }
}
